﻿using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI.Design;
using System.Web.UI.WebControls;

namespace Shp.Web.UI.WebControls.Iconized.Designers
{
   /// <summary>
   /// IconizedButton Designer base class.
   /// </summary>
   public abstract class IconizedButtonDesignerBase : ControlDesigner
   {
      #region Fields
      private string _webResourceSubstitutionPattern = @"(<%=)\s*(WebResource\("")(?<resourceName>.+)\s*(""\)%>)";
      private string _styleTemplate = "<style type=\"text/css\">{0}</style>";

      private List<string> _designTimecssResources = new List<string>()
      {
         "Shp.Web.UI.WebControls.Iconized.assets.css.shp.btn.designTime.css"
      };
      #endregion Fields

      #region Properties
      /// <summary>
      /// Gets the web resource substitution pattern.
      /// </summary>
      /// <value>The web resource substitution pattern.</value>
      public string WebResourceSubstitutionPattern
      {
         get { return this._webResourceSubstitutionPattern; }
      }

      /// <summary>
      /// Gets the style template.
      /// </summary>
      /// <value>The style template.</value>
      public string StyleTemplate
      {
         get { return this._styleTemplate; }
      }

      /// <summary>
      /// Gets the design time CSS resources.
      /// </summary>
      /// <value>The design time CSS resources.</value>
      public List<string> DesignTimeCssResources
      {
         get { return this._designTimecssResources; }
      }

      /// <summary>
      /// Gets the designed control.
      /// </summary>
      /// <value>The designed control.</value>
      protected abstract IIconizedButton DesignedControl { get; }
      #endregion Properties

      #region Methods
      /// <summary>
      /// Builds the design time HTML - including CSS Web Resources.
      /// </summary>
      /// <returns></returns>
      protected virtual string BuildDesignTimeHtml()
      {
         StringBuilder sbDesignTime = new StringBuilder();

         System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
         Regex regExWebResource = new Regex(this.WebResourceSubstitutionPattern);

         foreach (string resx in this.DesignTimeCssResources)
         {
            using (System.IO.Stream sResx = asm.GetManifestResourceStream(resx))
            using (System.IO.StreamReader srResx = new System.IO.StreamReader(sResx))
            {
               string css = srResx.ReadToEnd();
               css = regExWebResource.Replace(css, new MatchEvaluator(this.PerformSubstitution));

               sbDesignTime.AppendLine(string.Format(this.StyleTemplate, css));
            }
         }

         return sbDesignTime.ToString();
      }

      /// <summary>
      /// Performs the web resource substitution.
      /// </summary>
      /// <param name="m">The m.</param>
      /// <returns></returns>
      private string PerformSubstitution(Match m)
      {
         string replacedString = m.ToString();
         replacedString = replacedString.Replace(m.Value, (this.DesignedControl as WebControl).Page.ClientScript.GetWebResourceUrl(this.GetType(), m.Groups["resourceName"].Value));         
         return replacedString;
      }
      #endregion Methods

      #region Ctors
      #endregion Ctors
   }
}
